<template>
    <div class="layout-padding">
        <div v-if="!historyFlag">
            <el-button type="primary" @click="handleViewHistory"
                style="float: right;margin-bottom: 20px;">历史巡查记录</el-button>
            <el-table v-loading="loading" :data="list">
                <el-table-column type="index" width="80" label="序号"></el-table-column>
                <el-table-column label="预警时间" align="center" prop="warningTime" />
                <el-table-column label="预警状态" align="center" prop="warningType" />
                <el-table-column label="雨量计地点" align="center" prop="location" />

                <el-table-column label="最高降雨量" align="center">
                    <el-table-column label="10分钟/mm" align="center" prop="tenMins" />
                    <el-table-column label="1小时/mm" align="center" prop="anHour" />
                    <el-table-column label="日/mm" align="center" prop="oneDay" />
                </el-table-column>
                <el-table-column label="巡查单位" align="center" prop="unitName" />
                <el-table-column label="责任人" align="center" prop="responsibleUser" />
                <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" v-if="scope.row.status === 'notDispose'"
                            @click="handleDistributeOpen(scope.row)">下发</el-button>
                        <el-button link type="primary" v-else-if="scope.row.status === 'disposing'">已下发</el-button>
                        <el-button link type="primary" v-else-if="scope.row.status === 'disposed'"
                            @click="handleView(scope.row)">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="scope">
                        <el-tag type="danger" v-if="scope.row.status === 'notDispose'">未处置</el-tag>
                        <el-tag v-else-if="scope.row.status === 'disposing'">处置中</el-tag>
                        <el-tag type="success" v-else-if="scope.row.status === 'disposed'">已处置</el-tag>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>


        <div v-if="historyFlag">
            <el-form :inline="true" ref="queryRef" :model="queryParams" label-width="120px">
                <el-form-item label="巡查单位：">
                    <el-input placeholder="请输入内容" v-model="queryParams.unitName">
                    </el-input>
                </el-form-item>
                <el-form-item label="日期：">
                    <el-date-picker v-model="queryParams.time" type="datetimerange" range-separator="—"
                        start-placeholder="开始时间" end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss"
                        format="YYYY-MM-DD HH:mm:ss" @change="calendarChange" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
                    <el-button type="primary" @click="historyFlag = false">返回</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="list">
                <el-table-column type="index" width="80" label="序号"></el-table-column>
                <el-table-column label="日期" align="center" prop="warningTime" />
                <el-table-column label="雨量计地点" align="center" prop="location" />

                <el-table-column label="最高降雨量" align="center">
                    <el-table-column label="10分钟/mm" align="center" prop="tenMins" />
                    <el-table-column label="1小时/mm" align="center" prop="anHour" />
                    <el-table-column label="日/mm" align="center" prop="oneDay" />
                </el-table-column>
                <el-table-column label="巡查单位" align="center" prop="unitName" />
                <el-table-column label="巡查范围" align="center" prop="insRange" />
                <el-table-column label="巡查方式" align="center" prop="method" />
                <el-table-column label="出动人数/轨道车次数" align="center" prop="number" />
                <el-table-column label="发现问题" align="center" prop="problem" />
                <el-table-column label="反馈人" align="center" prop="responsibleUser" />
            </el-table>
        </div>



        <!-- 详情对话框 -->
        <el-dialog :title="title" v-model="open" width="1100px" append-to-body>
            <el-table v-loading="loading" :data="viewList">
                <el-table-column type="index" width="80" label="序号"></el-table-column>
                <el-table-column label="日期" align="center" prop="warningTime" />
                <el-table-column label="雨量计地点" align="center" prop="location" />
                <el-table-column label="最高降雨量" align="center">
                    <el-table-column label="10分钟/mm" align="center" prop="tenMins" />
                    <el-table-column label="1小时/mm" align="center" prop="anHour" />
                    <el-table-column label="日/mm" align="center" prop="oneDay" />
                </el-table-column>
                <el-table-column label="巡查单位" align="center" prop="unitName" />
                <el-table-column label="巡查范围" align="center" prop="insRange" />
                <el-table-column label="巡查方式" align="center" prop="method" />
                <el-table-column label="出动人数/轨道车次数" align="center" prop="number" />
                <el-table-column label="发现问题" align="center" prop="problem" />
                <el-table-column label="反馈人" align="center" prop="responsibleUser" />
            </el-table>
        </el-dialog>



        <!-- 下发对话框 -->
        <el-dialog title="下发" v-model="distributeOpen" width="500px" append-to-body>
            <el-form ref="postRef" :model="distributeForm" :rules="rules" label-width="auto">
                <el-form-item label="区段" prop="deptId">
                    <el-tree-select v-model="distributeForm.sectionId" :data="dataSource"
                        :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                        :render-after-expand="false" check-strictly @change="treeChange">
                    </el-tree-select>
                </el-form-item>
                <el-form-item label="工区" prop="workAreaId">
                    <el-tree-select v-model="distributeForm.workAreaId" :data="dataSourceArea"
                        :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                        :render-after-expand="false" check-strictly @change="workAreaChange">
                    </el-tree-select>
                </el-form-item>
                <el-form-item label="组员" prop="members">
                    <el-select v-model="distributeForm.members" placeholder="请选择" clearable multiple>
                        <el-option v-for="item in options" :key="item.userId" :label="item.userName"
                            :value="item.userId" />
                    </el-select>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="handleIssueRainfall">确 定</el-button>
                    <el-button @click="distributeOpen = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listRainfall, getRainfall, addRainfall, updateRainfall, delRainfall, issueRainfall, rainfallListHistory, viewRainfall } from "@/api/system/inspection.js";
import {getTreeList,getDeptBy,getPersonBy} from "@/api/work";
const { proxy } = getCurrentInstance();

const data = reactive({
    form: {},
    distributeForm:{},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        submited: true,
        unitName: undefined,
        time: [],
        params: {
            startTime: undefined,
            endTime: undefined,
        }
    },
    rules: {
        // section: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
    }
});
const { queryParams, form, rules,distributeForm } = toRefs(data);

const loading = ref(true);
const list = ref([]);
const viewList = ref([]);
const total = ref(0);
const open = ref(false);
const distributeOpen = ref(false);

const title = ref("降雨巡视");

const historyFlag = ref(false)

//区段数据
const dataSource = ref([])
const dataSourceArea = ref([])
const options = ref([])

function getTree() {
    getTreeList().then(res => {
        dataSource.value = res.data
    })
}
getTree()

function treeChange(id,b,v) {
    distributeForm.value.workAreaId = ''
    distributeForm.value.members = []
    getDeptBy({
      parentId: id
    }).then(res => {
      dataSourceArea.value = res.data
    })
  }

  function workAreaChange(id) {
    distributeForm.value.members = []
    getPersonBy({
      deptId: id
    }).then(res => {
      options.value = res.rows
    })
  }


/** 查询公告列表 */
function getList() {
    loading.value = true;
    listRainfall(queryParams.value).then(response => {
        list.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}


/** 搜索按钮操作 */
function handleQuery() {
    if (queryParams.value.time !== null && queryParams.value.time.length > 1) {
        queryParams.value.params.startTime = queryParams.value.time[0];
        queryParams.value.params.endTime = queryParams.value.time[1];
    }
    queryParams.value.pageNum = 1;
    getList();
}

/** 查看按钮操作 */
function handleView(row) {
    open.value = true
    viewRainfall({ id: row.id }).then(res => {
        viewList.value = res.rows
    })
}




/** 下发按钮操作 */
function handleDistributeOpen(row) {
    distributeOpen.value = true,
    distributeForm.value.id = row.id
}


function handleIssueRainfall(){
    issueRainfall({
        id: distributeForm.value.id,
        responsibleUserId: distributeForm.value.members.join(",") || "",
        unitId: distributeForm.value.sectionId,
    }).then(response => {
        distributeOpen.value = false
        getList();
        proxy.$modal.msgSuccess("下发成功");
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id;
    proxy.$modal.confirm('是否确认删除区段id为"' + id + '"的数据项？').then(function () {
        return delRainfall(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        sectionName: undefined,
        workAreaName: undefined,
        mileage: undefined,
        leftRight: undefined,
        location: undefined,
        netType: undefined,

        length: undefined,
        height: undefined,
        area: undefined,
        protectionType: undefined,
        remark: undefined,
        pic: undefined,
        picUrl: undefined
    };
    proxy.resetForm("configRef");
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["configRef"].validate(valid => {
        if (valid) {
            if (form.value.id != undefined) {
                updateRainfall(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addRainfall(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

// 历史巡查记录
function handleViewHistory() {
    historyFlag.value = true
}

function calendarChange(value) {
    if (value == null) {
        queryParams.value.params.startTime = undefined;
        queryParams.value.params.endTime = undefined;
    }
}

getList();

</script>

<style lang="scss" scoped>
.layout-padding {
    padding: 10px;
}
</style>
